<?php
/*
 * @ https://EasyToYou.eu - IonCube v11 Decoder Online
 * @ PHP 7.2 & 7.3
 * @ Decoder version: 1.1.6
 * @ Release: 10/08/2022
 */

// Decoded file for php version 71.
namespace app\massage\controller;

class IndexUser extends \app\ApiRest
{
    protected $model;
    protected $address_model;
    protected $coach_model;
    protected $coupon_record_model;
    protected $follow_model;
    public function __construct(\think\App $app)
    {
        parent::__construct($app);
        $this->model = new \app\massage\model\User();
        $this->address_model = new \app\massage\model\Address();
        $this->coach_model = new \app\massage\model\Coach();
        $this->coupon_record_model = new \app\massage\model\CouponRecord();
        $this->follow_model = new \app\dynamic\model\DynamicFollow();
    }
    public function attestationCoach()
    {
        $data = $this->model->dataInfo(["id" => $this->getUserId()]);
        $cap_dis[] = ["user_id", "=", $this->getUserId()];
        $cap_dis[] = ["status", "in", [1, 2, 3, 4]];
        $cap_info = $this->coach_model->where($cap_dis)->order("status")->find();
        $cap_info = !empty($cap_info) ? $cap_info->toArray() : [];
        $data["coach_status"] = !empty($cap_info) ? $cap_info["status"] : -1;
        if($data["coach_status"] == -1) {
            $this->coach_model->attestationCoach($data);
        }
        $cap_info = $this->coach_model->where($cap_dis)->order("status")->find();
        $cap_info = !empty($cap_info) ? $cap_info->toArray() : [];
        $data["coach_status"] = !empty($cap_info) ? $cap_info["status"] : -1;
        return $this->success($data);
    }
    public function index()
    {
//        var_dump($this->getUserInfo());
        if(!$this->getUserId()) {
            return $this->success([]);
        }
        $key = "userindex_user_index" . $this->getUserId();
        $value = getCache($key, $this->_uniacid);
        if(!empty($value)) {
            return $this->success($value);
        }
        $data = $this->model->dataInfo(["id" => $this->getUserId()]);
        $data["member_discount_auth"] = memberDiscountAuth($this->_uniacid)["status"];
        if($data["member_discount_auth"] == 1) {
            $data["member_status"] = time() < $data["member_discount_time"] ? 1 : 0;
            if($data["member_status"] == 1) {
                $member_card_model = new \app\memberdiscount\model\Card();
                $data["member_discount_title"] = $member_card_model->where(["id" => $data["member_discount_id"]])->value("title");
                $data["member_discount_day"] = ceil(($data["member_discount_time"] - time()) / 86400);
            }
        }
        $data["wallet_status"] = 1;
        $data = $this->model->authCheckData($data);
        $data["coupon_count"] = $this->coupon_record_model->couponCount($this->getUserId());
        $data["balance"] = getUserBalance($this->_user["id"], $this->_uniacid);
        if(in_array($data["coach_status"], [2, 3])) {
            $data["coach_level"] = $this->coach_model->getCoachLevel($data["coach_id"], $this->_uniacid);
        }
        $atv_model = new \app\massage\model\CouponAtv();
        $atv_record_model = new \app\massage\model\CouponAtvRecord();
        $dis = ["user_id" => $this->getUserId(), "status" => 1];
        $atv_ing = $atv_record_model->dataInfo($dis);
        $is_atv = 0;
        if(!empty($atv_ing)) {
            $is_atv = 1;
        } else {
            $atv_config = $atv_model->dataInfo(["uniacid" => $this->_uniacid]);
            $where[] = ["user_id", "=", $this->getUserId()];
            $where[] = ["status", "<>", 3];
            $count = $atv_record_model->where($where)->count();
            if($atv_config["status"] == 1 && $atv_config["start_time"] < time() && time() < $atv_config["end_time"] && $count < $atv_config["atv_num"]) {
                $is_atv = 1;
            }
        }
        $data["is_atv"] = $is_atv;
        $data["is_atv_status"] = $atv_model->where(["uniacid" => $this->_uniacid])->value("is_atv_status");
        $data["is_atv_status"] = !empty($data["is_atv_status"]) ? $data["is_atv_status"] : 0;
        $data["collect_count"] = $this->coach_model->coachCollectCount($this->getUserId(), $this->_uniacid);
        $data["follow_count"] = $this->follow_model->followCoachNum($this->getUserId());
        $level_model = new \app\member\model\Level();
        $level_model->initMemberRights($this->getUserId());
        setCache($key, $data, 3, $this->_uniacid);
        return $this->success($data);
    }
    public function getUserCoachStatus()
    {
        if(!$this->getUserId()) {
            return $this->success(["status" => -1]);
        }
        $key = "getUserCoachStatus_key" . $this->getUserId();
        $value = getCache($key, $this->_uniacid);
        if(!empty($value)) {
            return $this->success($value);
        }
        $cap_dis[] = ["user_id", "=", $this->getUserId()];
        $cap_dis[] = ["status", "in", [1, 2, 3, 4]];
        $coach_model = new \app\massage\model\Coach();
        $coach_info = $coach_model->where($cap_dis)->order("id desc")->field("id as coach_id,lng,lat,address,coach_position,status")->find();
        if(empty($coach_info)) {
            $coach_info = ["status" => -1];
        }
        setCache($key, $coach_info, 3, $this->_uniacid);
        return $this->success($coach_info);
    }
    public function addressList()
    {
        $dis[] = ["user_id", "=", $this->getUserId()];
        $dis[] = ["status", ">", -1];
        $data = $this->address_model->dataList($dis, 10);
        return $this->success($data);
    }
    public function addressInfo()
    {
        $input = $this->_param;
        $dis = ["id" => $input["id"]];
        $data = $this->address_model->dataInfo($dis);
        return $this->success($data);
    }
    public function addressAdd()
    {
        $input = $this->_input;
        $input["uniacid"] = $this->_uniacid;
        $input["user_id"] = $this->getUserId();
        if(empty($input["area"])) {
            $city_data = getCityByLat($input["lng"], $input["lat"], $this->_uniacid);
            $input = array_merge($city_data, $input);
        }
        if(!empty($input["phone_code"])) {
            $phone_code = getCache($input["mobile"], $this->_uniacid);
            if($input["phone_code"] != $phone_code) {
                return $this->error("验证码错误");
            }
            delCache($input["phone_code"], $this->_uniacid);
            unset($input["phone_code"]);
        }
        $res = $this->address_model->dataAdd($input);
        if($input["status"] == 1) {
            $id = $this->address_model->getLastInsID();
            $this->address_model->updateOne($id);
        }
        return $this->success($res);
    }
    public function addressUpdate()
    {
        $input = $this->_input;
        $dis = ["id" => $input["id"]];
        if(empty($input["area"]) && !empty($input["lng"])) {
            $city_data = getCityByLat($input["lng"], $input["lat"], $this->_uniacid);
            $input = array_merge($city_data, $input);
        }
        if(!empty($input["phone_code"])) {
            $phone_code = getCache($input["mobile"], $this->_uniacid);
            if($input["phone_code"] != $phone_code) {
                return $this->error("验证码错误");
            }
            delCache($input["phone_code"], $this->_uniacid);
            unset($input["phone_code"]);
        }
        $res = $this->address_model->dataUpdate($dis, $input);
        if(!empty($input["status"]) && $input["status"] == 1) {
            $this->address_model->updateOne($input["id"]);
        }
        return $this->success($res);
    }
    public function getDefultAddress()
    {
        $address_model = new \app\massage\model\Address();
        $address = $address_model->dataInfo(["user_id" => $this->getUserId(), "status" => 1]);
        return $this->success($address);
    }
    public function addressDel()
    {
        $input = $this->_input;
        $dis = ["id" => $input["id"]];
        $res = $this->address_model->where($dis)->delete();
        return $this->success($res);
    }
    public function userUpdate()
    {
        $input = $this->_input;
        $dis = ["id" => $this->getUserId()];
        if(!empty($input["coupon_atv_id"]) && empty($this->getUserInfo()["nickName"]) && !empty($input["nickName"])) {
            $coupon_atv_model = new \app\massage\model\CouponAtv();
            $coupon_atv_model->invUser($this->getUserId(), $input["coupon_atv_id"]);
        }
        if($this->is_app == 0) {
            $setting = new \longbingcore\wxcore\WxSetting($this->_uniacid);
            $res = $setting->checkKeyWordsv2($input["nickName"]);
            if(!$res) {
                return $this->error("昵称含有敏感违禁词");
            }
        }
        $update = ["nickName" => $input["nickName"], "gender" => !empty($input["gender"]) ? $input["gender"] : "", "language" => !empty($input["language"]) ? $input["language"] : "", "city" => !empty($input["city"]) ? $input["city"] : "", "province" => !empty($input["province"]) ? $input["province"] : "", "country" => !empty($input["country"]) ? $input["country"] : "", "avatarUrl" => !empty($input["avatarUrl"]) ? $input["avatarUrl"] : ""];
        $res = $this->model->dataUpdate($dis, $update);
        if(!empty($input["encryptedData"])) {
            $encryptedData = $input["encryptedData"];
            $iv = $input["iv"];
            $config = longbingGetAppConfig($this->_uniacid);
            $appid = $config["appid"];
            $session_key = $this->model->where(["id" => $this->getUserId()])->value("session_key");
            if(empty($session_key)) {
                $this->errorMsg("need login", 401);
            }
            $datas = NULL;
            $errCode = decryptDataLongbing($appid, $session_key, $encryptedData, $iv, $datas);
            if($errCode == 0) {
                $data = json_decode($datas, true);
                $unionid = !empty($data["unionid"]) ? $data["unionid"] : "";
                if(!empty($unionid)) {
                    $dis = ["unionid" => $unionid, "uniacid" => $this->uniacid];
                    $find = $this->model->dataInfo($dis);
                    if(!empty($find)) {
                        $this->errorMsg("need login", 401);
                    } else {
                        $this->model->dataUpdate(["id" => $this->getUserId()], ["unionid" => $unionid]);
                    }
                }
            } else {
                return $this->error($errCode);
            }
        }
        $user_info = $this->model->dataInfo(["id" => $this->getUserId()]);
        setCache($this->autograph, $user_info, 7200, $this->_uniacid);
        return $this->success($res);
    }
    public function userInfo()
    {
        $data = $this->model->dataInfo(["id" => $this->getUserId()]);
        $cap_dis[] = ["user_id", "=", $this->getUserId()];
        $cap_dis[] = ["status", "in", [1, 2, 3, 4]];
        $cap_info = $this->coach_model->where($cap_dis)->order("status")->find();
        $cap_info = !empty($cap_info) ? $cap_info->toArray() : [];
        $data["coach_status"] = !empty($cap_info) ? $cap_info["status"] : -1;
        $data["coach_position"] = !empty($cap_info["coach_position"]) && $data["coach_status"] == 2 ? 1 : 0;
        $distri_model = new \app\massage\model\DistributionList();
        $fx = $distri_model->dataInfo($cap_dis);
        $data["fx_status"] = !empty($fx) ? $fx["status"] : -1;
        $data["fx_text"] = !empty($fx) ? $fx["sh_text"] : "";
        $channel_model = new \app\massage\model\ChannelList();
        $channel = $channel_model->dataInfo($cap_dis);
        $data["channel_status"] = !empty($channel) ? $channel["status"] : -1;
        $data["channel_text"] = !empty($channel) ? $channel["sh_text"] : "";
        $data["tax_point"] = getConfigSetting($this->_uniacid, "tax_point");
        $auth = \longbingcore\permissions\AdminMenu::getAuthList((int) $this->_uniacid, ["heepay", "adapay"]);
        if($auth["adapay"]) {
            $adapay_config = new \app\adapay\model\Config();
            $config_data = $adapay_config->dataInfo(["uniacid" => $this->_uniacid]);
            $auth["adapay"] = !empty($config_data["status"]) ? $auth["adapay"] : 0;
        }
        if($auth["heepay"] && $auth["adapay"] != 1) {
            $adapay_config = new \app\heepay\model\Config();
            $config_data = $adapay_config->dataInfo(["uniacid" => $this->_uniacid]);
            $auth["heepay"] = !empty($config_data["status"]) ? $auth["heepay"] : 0;
        }
        $data["bank_card_id"] = "";
        $data["bank_status"] = -1;
        if($auth["adapay"] == 1) {
            $adapay_member_model = new \app\adapay\model\Member();
            $adapay_bank_model = new \app\adapay\model\Bank();
            $adapay_member = $adapay_member_model->where(["user_id" => $this->getUserId()])->where("status", ">", -1)->find();
            if(!empty($adapay_member)) {
                $data["bank_card_id"] = $adapay_bank_model->where(["order_member_id" => $adapay_member["id"]])->value("card_id");
            }
            $data["bank_status"] = $adapay_member["status"];
        }
        if($auth["heepay"] == 1) {
            $member = new \app\heepay\model\Member();
            $bank_card_id = $member->where(["user_id" => $this->getUserId()])->where("status", ">", -1)->field("bank_card_no,audit_status as status")->find();
            $data["bank_card_id"] = $bank_card_id["bank_card_no"];
            $data["bank_status"] = $bank_card_id["status"];
        }
        return $this->success($data);
    }
    public function coachInfo()
    {
        if(!$this->getUserId()) {
            return $this->success([]);
        }
        $order_model = new \app\massage\model\Order();
        $order_model->coachBalanceArr($this->_uniacid);
        $cap_dis[] = ["user_id", "=", $this->getUserId()];
        $cap_dis[] = ["status", "in", [1, 2, 3, 4]];
        $cap_info = $this->coach_model->dataInfo($cap_dis);
        $city_model = new \app\massage\model\City();
        if(!empty($cap_info)) {
            $cap_info["city"] = $city_model->where(["id" => $cap_info["city_id"]])->value("title");
            $coach_level = $this->coach_model->getCoachLevel($cap_info["id"], $this->_uniacid);
            $cap_info["coach_level"] = $this->coach_model->coachLevelInfo($coach_level);
            $cap_info["text_type"] = $this->coach_model->getCoachWorkStatus($cap_info["id"], $this->_uniacid);
            $record_model = new \app\fdd\model\FddAgreementRecord();
            $config_model = new \app\fdd\model\FddConfig();
            $dis = ["user_id" => $this->_user["id"], "status" => 2, "admin_id" => $cap_info["admin_id"]];
            $fdd_agreement = $record_model->where($dis)->field("download_url,viewpdf_url,end_time")->order("id desc")->find();
            $dis["status"] = 4;
            $cap_info["fdd_agreement"] = $record_model->where($dis)->field("download_url,viewpdf_url,end_time")->order("id desc")->find();
            $fdd_status = $config_model->getStatus($this->_uniacid);
            $cap_info["fdd_auth_status"] = $fdd_status;
            if($fdd_status == 1) {
                if(!empty($fdd_agreement)) {
                    $cap_info["fdd_status"] = 1;
                } else {
                    $cap_info["fdd_status"] = 0;
                }
            } else {
                $cap_info["fdd_status"] = 2;
            }
            $cap_info["address"] = getCoachAddress($cap_info["lng"], $cap_info["lat"], $cap_info["uniacid"], $cap_info["id"]);
            $industry_model = new \app\industrytype\model\Type();
            $cap_info["industry_info"] = $industry_model->where(["id" => $cap_info["industry_type"], "status" => 1])->find();
        }
        $level_model = new \app\massage\model\CoachLevel();
        $cap_info["max_level"] = $level_model->where(["uniacid" => $this->_uniacid, "status" => 1])->max("balance");
        return $this->success($cap_info);
    }
    public function coachApply()
    {
        $input = $this->_input;
        if(!empty($input["short_code"])) {
            $short_code = getCache($input["mobile"], $this->_uniacid);
            if($input["short_code"] != $short_code) {
                return $this->error("验证码错误");
            }
            unset($input["short_code"]);
        }
        $res = $this->coach_model->coachApply($input, $this->getUserId(), $this->_uniacid);
        if(!empty($res["code"])) {
            $this->errorMsg($res["msg"]);
        }
        setCache($input["mobile"], "", 99, $this->_uniacid);
        return $this->success($res);
    }
    public function coachCollectList()
    {
        $input = $this->_param;
        $config_model = new \app\massage\model\ConfigSetting();
        $config = $config_model->dataInfo($this->_uniacid);
        $collect_model = new \app\massage\model\CoachCollect();
        if(in_array($config["coach_format"], [1, 3])) {
            $data = $collect_model->coachCollectListTypeOne($input, $this->_user["id"], $this->_uniacid);
        } else {
            $data = $collect_model->coachCollectListTypeTow($input, $this->_user["id"], $this->_uniacid);
        }
        return $this->success($data);
    }
    public function addCollect()
    {
        $input = $this->_input;
        $insert = ["uniacid" => $this->_uniacid, "coach_id" => $input["coach_id"], "user_id" => $this->getUserId()];
        $collect_model = new \app\massage\model\CoachCollect();
        $res = $collect_model->dataAdd($insert);
        return $this->success($res);
    }
    public function delCollect()
    {
        $input = $this->_input;
        $dis = ["uniacid" => $this->_uniacid, "coach_id" => $input["coach_id"], "user_id" => $this->getUserId()];
        $collect_model = new \app\massage\model\CoachCollect();
        $res = $collect_model->where($dis)->delete();
        return $this->success($res);
    }
    public function userCouponList()
    {
        $input = $this->_param;
        $this->coupon_record_model->initCoupon($this->_uniacid);
        $dis = ["user_id" => $this->getUserId(), "status" => $input["status"], "is_show" => 1];
        $data = $this->coupon_record_model->dataList($dis);
        $coupon_model = new \app\massage\model\Coupon();
        if(!empty($data["data"])) {
            foreach ($data["data"] as &$v) {
                $v["send_type"] = $coupon_model->where(["id" => $v["coupon_id"]])->value("send_type");
                $v["start_time"] = date("Y.m.d H:i", $v["start_time"]) . " - " . date("Y.m.d H:i", $v["end_time"]);
            }
        }
        return $this->success($data);
    }
    public function couponDel()
    {
        $input = $this->_input;
        $coupon = $this->coupon_record_model->dataInfo(["id" => $input["coupon_id"]]);
        if($coupon["status"] == 1) {
            $this->errorMsg("待使用待卡券不能删除");
        }
        $res = $this->coupon_record_model->dataUpdate(["id" => $input["coupon_id"]], ["is_show" => 0]);
        return $this->success($res);
    }
    public function couponAtvInfo()
    {
        $input = $this->_input;
        $atv_record_model = new \app\massage\model\CouponAtvRecord();
        $atv_record_list_model = new \app\massage\model\CouponAtvRecordList();
        $atv_model = new \app\massage\model\CouponAtv();
        if(empty($input["id"])) {
            $dis_where[] = ["status", "=", 1];
            $dis_where[] = ["end_time", "<", time()];
            $atv_record_model->dataUpdate($dis_where, ["status" => 3]);
            $dis = ["user_id" => $this->getUserId(), "status" => 1];
            $atv_ing = $atv_record_model->dataInfo($dis);
            if(empty($atv_ing)) {
                $atv_ing = $this->couponAtvAdd();
            }
            if(empty($atv_ing)) {
                $atv_ing = $atv_record_model->where(["user_id" => $this->getUserId()])->order("id desc")->find();
                $atv_ing = !empty($atv_ing) ? $atv_ing->toArray() : [];
            }
            if(empty($atv_ing)) {
                $this->errorMsg("你没有可以进行的活动");
            }
        } else {
            $dis = ["id" => $input["id"]];
            $atv_ing = $atv_record_model->dataInfo($dis);
        }
        $atv = $atv_model->dataInfo(["uniacid" => $this->_uniacid]);
        $atv_ing["atv_num"] = $atv["atv_num"];
        $atv_ing["end_time"] -= time();
        $atv_ing["end_time"] = 0 < $atv_ing["end_time"] ? $atv_ing["end_time"] : 0;
        $data["atv_info"] = $atv_ing;
        $data["record_list"] = $atv_record_list_model->dataList(["a.record_id" => $atv_ing["id"]], 50);
        return $this->success($data);
    }
    public function couponAtvAdd()
    {
        $atv_model = new \app\massage\model\CouponAtv();
        $atv_record_model = new \app\massage\model\CouponAtvRecord();
        $atv_record_coupon_model = new \app\massage\model\CouponAtvRecordCoupon();
        $atv_config = $atv_model->dataInfo(["uniacid" => $this->_uniacid]);
        if($atv_config["status"] == 0) {
            return [];
        }
        if(time() < $atv_config["start_time"] || $atv_config["end_time"] < time()) {
            return [];
        }
        if(empty($atv_config["coupon"])) {
            return [];
        }
        $where[] = ["user_id", "=", $this->getUserId()];
        $where[] = ["status", "<>", 3];
        $count = $atv_record_model->where($where)->count();
        if($atv_config["atv_num"] <= $count) {
            return [];
        }
        $insert = ["uniacid" => $this->_uniacid, "user_id" => $this->getUserId(), "atv_id" => $atv_config["id"], "atv_start_time" => $atv_config["start_time"], "atv_end_time" => $atv_config["end_time"], "inv_user_num" => $atv_config["inv_user_num"], "inv_time" => $atv_config["inv_time"], "start_time" => time(), "end_time" => time() + $atv_config["inv_time"] * 3600, "inv_user" => $atv_config["inv_user"], "to_inv_user" => $atv_config["to_inv_user"], "share_img" => $atv_config["share_img"]];
        \think\facade\Db::startTrans();
        $res = $atv_record_model->dataAdd($insert);
        if($res == 0) {
            \think\facade\Db::rollback();
            $this->errorMsg("发起活动失败");
        }
        $record_id = $atv_record_model->getLastInsID();
        foreach ($atv_config["coupon"] as $value) {
            $insert = ["uniacid" => $this->_uniacid, "atv_id" => $atv_config["id"], "record_id" => $record_id, "coupon_id" => $value["coupon_id"], "num" => $value["num"]];
            $res = $atv_record_coupon_model->dataAdd($insert);
            if($res == 0) {
                \think\facade\Db::rollback();
                $this->errorMsg("发起活动失败");
            }
        }
        \think\facade\Db::commit();
        $record = $atv_record_model->dataInfo(["id" => $record_id]);
        return $record;
    }
    public function atvQr()
    {
        $input = $this->_input;
        $key = "atv_coupon" . $input["coupon_atv_id"];
        $qr = getCache($key, $this->_uniacid);
        if(empty($qr)) {
            $qr = $this->model->orderQr($input, $this->_uniacid);
            setCache($key, $qr, 86400, $this->_uniacid);
        }
        $qr = !empty($qr) ? $qr : $this->defaultImage["image"];
        return $this->success($qr);
    }
    public function reportPhone()
    {
        $params = $this->_input;
        $encryptedData = $params["encryptedData"];
        if(!empty($params["decode"])) {
            $encryptedData = rawurldecode($encryptedData);
        }
        $iv = $params["iv"];
        $config = longbingGetAppConfig($this->_uniacid);
        $appid = $config["appid"];
        $session_key = $this->model->where(["id" => $this->getUserId()])->value("session_key");
        if(empty($session_key)) {
            $this->errorMsg("need login", 401);
        }
        $data = NULL;
        $errCode = decryptDataLongbing($appid, $session_key, $encryptedData, $iv, $data);
        if($errCode == 0) {
            $data = json_decode($data, true);
            $phone = $data["purePhoneNumber"];
            $res = $this->model->dataUpdate(["id" => $this->getUserId()], ["phone" => $phone]);
            $user_info = $this->model->dataInfo(["id" => $this->getUserId()]);
            setCache($this->autograph, $user_info, 7200, $this->_uniacid);
            return $this->success($phone);
        }
        return $this->error($errCode);
    }
    public function commList()
    {
        $input = $this->_param;
        $limit = !empty($input["limit"]) ? $input["limit"] : 10;
        $dis[] = ["a.uniacid", "=", $this->_uniacid];
        $dis[] = ["a.cash", ">", 0];
        $dis[] = ["a.top_id", "=", $this->getUserId()];
        $dis[] = ["a.type", "=", 1];
        if(!empty($input["status"])) {
            $dis[] = ["a.status", "=", $input["status"]];
        } else {
            $dis[] = ["a.status", ">", -1];
        }
        $comm_model = new \app\massage\model\Commission();
        $order_model = new \app\massage\model\Order();
        $coach_model = new \app\massage\model\Coach();
        $data = $comm_model->recordList($dis, $limit);
        $data["total_cash"] = $comm_model->where(["top_id" => $this->getUserId()])->where("type", "in", [1, 9])->where("status", ">", -1)->sum("cash");
        $data["total_cash"] = round($data["total_cash"], 2);
        if(!empty($data["data"])) {
            foreach ($data["data"] as &$v) {
                $coach_id = $order_model->where(["id" => $v["order_id"]])->value("coach_id");
                $v["coach_name"] = $coach_model->where(["id" => $coach_id])->value("coach_name");
                $v["nickName"] = $this->model->where(["id" => $v["user_id"]])->value("nickName");
                $v["create_time"] = date("Y-m-d H:i:s", $v["create_time"]);
            }
        }
        return $this->success($data);
    }
    public function base64ToImg()
    {
        $input = $this->_input;
        $image = $input["img"];
        $imageName = "25220_" . date("His", time()) . "_" . rand(1111, 9999) . ".png";
        if(strstr($image, ",")) {
            $image = explode(",", $image);
            $image = $image[1];
        }
        $base_path = "/image/" . $this->uniacid . "/" . date("y") . "/" . date("m");
        $path = FILE_UPLOAD_PATH . $base_path;
        if(!is_dir($path)) {
            mkdir($path, 511, true);
        }
        $imageSrc = $path . "/" . $imageName;
        $r = file_put_contents($imageSrc, base64_decode($image));
        $img = UPLOAD_PATH . $imageSrc;
        return $this->success($img);
    }
    public function adminCoachQr()
    {
        $input = $this->_param;
        $admin_model = new \app\massage\model\Admin();
        $dis = ["user_id" => $this->getUserId(), "status" => 1];
        $admin_user = $admin_model->dataInfo($dis);
        if(empty($admin_user)) {
            $this->errorMsg("你还不是加盟商");
        }
        $key = "join_admin" . $admin_user["id"] . "-" . $this->is_app;
        $qr = getCache($key, $this->_uniacid);
        if(empty($qr)) {
            if($this->is_app == 0) {
                $input["page"] = "technician/pages/apply";
                $input["admin_id"] = $admin_user["id"];
                $qr = $this->model->orderQr($input, $this->_uniacid);
            } else {
                $page = "https://" . $_SERVER["HTTP_HOST"] . "/h5/#/technician/pages/apply?admin_id=" . $admin_user["id"];
                $qr = base64ToPng(getCode($this->_uniacid, $page));
            }
            setCache($key, $qr, 86400, $this->_uniacid);
        }
        $qr = !empty($qr) ? $qr : "https://" . $_SERVER["HTTP_HOST"] . "/favicon.ico";
        return $this->success($qr);
    }
    public function userCashInfo()
    {
        $user_info = $this->model->dataInfo(["id" => $this->getUserId()]);
        $data["new_cash"] = $user_info["new_cash"];
        $dis = ["top_id" => $this->getUserId(), "type" => 1];
        $comm_model = new \app\massage\model\Commission();
        $wallet_model = new \app\massage\model\Wallet();
        $data["total_cash"] = $comm_model->where($dis)->where("status", ">", -1)->sum("cash");
        $dis["status"] = 1;
        $data["unrecorded_cash"] = $comm_model->where($dis)->sum("cash");
        $data["unrecorded_cash"] = round($data["unrecorded_cash"], 2);
        $data["total_cash"] = round($data["total_cash"], 2);
        $data["extract_total_price"] = $wallet_model->userCash($this->getUserId(), 2);
        $data["extract_ing_price"] = $wallet_model->userCash($this->getUserId(), 1);
        return $this->success($data);
    }
    public function applyWallet()
    {
        $input = $this->_input;
        $user_info = $this->model->dataInfo(["id" => $this->getUserId()]);
        $new_cash = $user_info["new_cash"];
        if(empty($input["apply_price"]) || $input["apply_price"] < 0) {
            $this->errorMsg("提现费最低一分");
        }
        if($new_cash < $input["apply_price"]) {
            $this->errorMsg("余额不足");
        }
        $reseller_model = new \app\massage\model\DistributionList();
        $admin_id = $reseller_model->where(["user_id" => $this->getUserId(), "status" => 2])->value("admin_id");
        $tax_point = getConfigSetting($this->_uniacid, "tax_point");
        $balance = 100 - $tax_point;
        $key = "user_wallet" . $this->getUserId();
        $value = getCache($key, $this->_uniacid);
        if(!empty($value)) {
            $this->errorMsg("网络错误，请刷新重试");
        }
        incCache($key, 1, $this->_uniacid);
        \think\facade\Db::startTrans();
        $res = $this->model->dataUpdate(["id" => $this->getUserId(), "lock" => $user_info["lock"]], ["new_cash" => $user_info["new_cash"] - $input["apply_price"], "lock" => $user_info["lock"] + 1]);
        if($res != 1) {
            \think\facade\Db::rollback();
            delCache($key, $this->_uniacid);
            $this->errorMsg("申请失败");
        }
        $insert = ["uniacid" => $this->_uniacid, "user_id" => $this->getUserId(), "coach_id" => 0, "admin_id" => !empty($admin_id) ? $admin_id : 0, "total_price" => $input["apply_price"], "balance" => $balance, "apply_price" => round($input["apply_price"] * $balance / 100, 2), "service_price" => round($input["apply_price"] * $tax_point / 100, 2), "tax_point" => $tax_point, "code" => orderCode(), "text" => $input["text"], "type" => 4, "last_login_type" => $this->is_app, "apply_transfer" => !empty($input["apply_transfer"]) ? $input["apply_transfer"] : 0];
        $wallet_model = new \app\massage\model\Wallet();
        $res = $wallet_model->dataAdd($insert);
        if($res != 1) {
            \think\facade\Db::rollback();
            delCache($key, $this->_uniacid);
            $this->errorMsg("申请失败");
        }
        \think\facade\Db::commit();
        delCache($key, $this->_uniacid);
        return $this->success($res);
    }
    public function walletList()
    {
        $wallet_model = new \app\massage\model\Wallet();
        $input = $this->_param;
        $dis[] = ["user_id", "=", $this->getUserId()];
        if(!empty($input["status"])) {
            $dis[] = ["status", "=", $input["status"]];
        }
        $resller_model = new \app\massage\model\DistributionList();
        $del_time = $resller_model->where(["user_id" => $this->getUserId(), "status" => -1])->max("del_time");
        if(!empty($del_time)) {
            $dis[] = ["create_time", ">", $del_time];
        }
        $dis[] = ["type", "=", 4];
        $data = $wallet_model->dataList($dis, 10);
        if(!empty($data["data"])) {
            foreach ($data["data"] as &$v) {
                $v["create_time"] = date("Y-m-d H:i:s", $v["create_time"]);
            }
        }
        $data["extract_total_price"] = $wallet_model->userCash($this->getUserId(), 2, $del_time);
        $data["personal_income_tax_text"] = getConfigSetting($this->_uniacid, "personal_income_tax_text");
        return $this->success($data);
    }
    public function myTeam()
    {
        $user_model = new \app\massage\model\User();
        $dis = ["a.pid" => $this->getUserId()];
        $data = $user_model->alias("a")->join("massage_service_order_list b", "a.id = b.user_id AND b.pay_type > 1", "left")->where($dis)->field("a.id,a.city,a.area,a.province,a.create_time,a.nickName,a.avatarUrl,ifnull(SUM(b.true_service_price),0) as pay_price,ifnull(COUNT(b.id),0) as order_count")->group("a.id")->order("a.id desc")->paginate(10)->toArray();
        if(!empty($data["data"])) {
            foreach ($data["data"] as &$v) {
                $v["create_time"] = date("Y-m-d H:i:s", $v["create_time"]);
                $v["pay_price"] = round($v["pay_price"], 2);
            }
        }
        return $this->success($data);
    }
    public function userCommQr()
    {
        $input = $this->_param;
        $type = getConfigSetting($this->_uniacid, "wechat_qr_type");
        $key = "user_commsssssss" . $this->getUserId() . "-" . $this->is_app . "-" . $type;
        $qr = getCache($key, 99999999);
        if(empty($qr)) {
            if($this->is_app == 0) {
                $input["page"] = "user/pages/gzh";
                $input["pid"] = $this->getUserId();
                $qr = $this->model->orderQr($input, $this->_uniacid);
            } elseif($type == 0) {
                $page = "https://" . $_SERVER["HTTP_HOST"] . "/h5/#/user/pages/gzh?pid=" . $this->getUserId();
                $qr = base64ToPng(getCode($this->_uniacid, $page));
            } else {
                $core = new \longbingcore\wxcore\WxSetting($this->_uniacid);
                $db_code_do = getConfigSetting($this->_uniacid, "db_code_do");
                if($db_code_do == 1) {
                    $qr = $core->qrCode($this->getUserId() . ",aa");
                } else {
                    $qr = $core->qrCode($this->getUserId());
                }
            }
            setCache($key, $qr, 864000000, 99999999);
        }
//        var_dump($qr);
        $qr = !empty($qr) ? $qr : "https://" . $_SERVER["HTTP_HOST"] . "/favicon.ico";
        return $this->success($qr);
    }
    public function applyReseller()
    {
        $input = $this->_input;
        $distribution_model = new \app\massage\model\DistributionList();
        $dis[] = ["status", ">", -1];
        $dis[] = ["user_id", "=", $this->getUserId()];
        $find = $distribution_model->dataInfo($dis);
        if(!empty($find) && in_array($find["status"], [1, 2, 3])) {
            $this->errorMsg("你已经申请");
        }
        $auth = $distribution_model->getPayResellerAuth($this->_uniacid);

        if(!empty($input["level_reseller_id"])) {
            $top = $distribution_model->where(["id" => $input["level_reseller_id"]])->where("status", "in", [2, 3])->find();
        }
        $insert = ["uniacid" => $this->_uniacid, "user_id" => $this->getUserId(), "user_name" => $input["user_name"], "true_user_name" => !empty($input["true_user_name"]) ? $input["true_user_name"] : $input["user_name"], "mobile" => $input["mobile"], "text" => $input["text"], "status" => $auth ? -1 : 1, "pid" => !empty($top) ? $top["id"] : 0, "admin_id" => !empty($input["admin_id"]) ? $input["admin_id"] : 0];
        if(!$auth && getConfigSetting($this->_uniacid, "reseller_check_type") == 1) {
            $insert["status"] = 2;
            $insert["sh_time"] = time();
        }
        \think\facade\Db::startTrans();
        if(!empty($top)) {
            $insert["admin_id"] = $top["admin_id"];
        }
        if(!empty($find) && $find["status"] == 4) {
            $insert["status"] = $auth ? 4 : $insert["status"];
            $res = $distribution_model->dataUpdate(["id" => $find["id"]], $insert);
            $id = $find["id"];
        } else {
            $res = $distribution_model->dataAdd($insert);
            $id = $distribution_model->getLastInsID();
        }
        if($auth) {
            if(empty($input["pay_reseller_type"])) {
                \think\facade\Db::rollback();
                $this->errorMsg("请选择套餐");
            }
            $config = getConfigSettingArr($this->_uniacid, ["reseller_threshold", "level_reseller_threshold", "reseller_inv_balance", "level_reseller_inv_balance", "wx_point", "ali_point"]);
            $pay_price = $input["pay_reseller_type"] == 1 ? $config["reseller_threshold"] : $config["level_reseller_threshold"];
            $order_insert = ["uniacid" => $this->_uniacid, "user_id" => $this->_user["id"], "reseller_id" => $id, "pay_price" => $pay_price, "order_code" => orderCode(), "type" => $input["pay_reseller_type"], "pay_model" => $input["pay_model"], "app_pay" => $this->is_app];
            if(!empty($top)) {
                $order_insert["top_reseller_id"] = $top["id"];
                $order_insert["top_user_id"] = $top["user_id"];
            }
            $order_model = new \app\payreseller\model\Order();
            $order_model->dataAdd($order_insert);
            $order_id = $order_model->getLastInsID();
            if(!empty($top)) {
                $balance = $input["pay_reseller_type"] == 1 ? $config["reseller_inv_balance"] : $config["level_reseller_inv_balance"];
                $cash = round($order_insert["pay_price"] * $balance / 100, 2);
                if($input["pay_model"] == 3) {
                    $point = $config["ali_point"];
                } else {
                    $point = $config["wx_point"];
                }
                $point_cash = round($cash * $point / 100, 2);
                $cash -= $point_cash;
                $comm_insert = ["uniacid" => $this->_uniacid, "user_id" => $this->_user["id"], "top_id" => $order_insert["top_user_id"], "order_id" => $order_id, "order_code" => $order_insert["order_code"], "cash" => $cash, "type" => 15, "balance" => $balance, "admin_id" => !empty($input["admin_id"]) ? $input["admin_id"] : 0, "sub_reseller_id" => $id, "reseller_id" => $order_insert["top_reseller_id"], "status" => -1];
                $comm_model = new \app\massage\model\Commission();
                $comm_model->dataAdd($comm_insert);
                $comm_id = $comm_model->getLastInsID();
                $share_data = ["pay_point" => $point, "inv_reseller_point_cash" => $point_cash, "id" => $order_id, "uniacid" => $this->_uniacid];
                $share_model = new \app\massage\model\CommShare();
                $share_model->addPointData($comm_id, $share_data, $comm_insert["type"], $comm_insert["top_id"]);
            }
        }
        \think\facade\Db::commit();
        if($auth) {
            if($order_insert["pay_price"] <= 0) {
                $order_model->orderResult($order_insert["order_code"], $order_insert["order_code"]);
                return $this->success(true);
            }
            if($input["pay_model"] == 3) {
                $pay_model = new \longbingcore\wxcore\PayModel($this->payConfig());
                $jsApiParameters = $pay_model->aliPay($order_insert["order_code"], $order_insert["pay_price"], "ResellerPay", 4);
                $arr["pay_list"] = $jsApiParameters;
                $arr["order_code"] = $order_insert["order_code"];
                $arr["order_id"] = $order_id;
            } else {
                $pay_controller = new \app\shop\controller\IndexWxPay($this->app);
                $jsApiParameters = $pay_controller->createWeixinPay($this->payConfig(), $this->getUserInfo()["openid"], $this->_uniacid, "reseller", ["type" => "ResellerPay", "out_trade_no" => $order_insert["order_code"], "order_id" => (string) $order_id], $order_insert["pay_price"]);
                $arr["pay_list"] = $jsApiParameters;
                $arr["order_id"] = $order_id;
            }
            return $this->success($arr);
        }
        return $this->success($res);
    }
    public function reApplyResellerOrder()
    {
        $input = $this->_input;
        $order_model = new \app\payreseller\model\Order();
        $order_insert = $order_model->dataInfo(["id" => $input["id"]]);
        $order_id = $input["id"];
        if($order_insert["pay_model"] == 3) {
            $pay_model = new \longbingcore\wxcore\PayModel($this->payConfig());
            $jsApiParameters = $pay_model->aliPay($order_insert["order_code"], $order_insert["pay_price"], "ResellerPay", 4, ["openid" => $this->getUserInfo()["openid"], "uniacid" => $this->_uniacid, "type" => "ResellerPay", "out_trade_no" => $order_insert["order_code"], "order_id" => (string) $order_id]);
            $arr["pay_list"] = $jsApiParameters;
            $arr["order_code"] = $order_insert["order_code"];
            $arr["order_id"] = $order_id;
        } else {
            $pay_controller = new \app\shop\controller\IndexWxPay($this->app);
            $jsApiParameters = $pay_controller->createWeixinPay($this->payConfig(), $this->getUserInfo()["openid"], $this->_uniacid, "reseller", ["type" => "ResellerPay", "out_trade_no" => $order_insert["order_code"], "order_id" => (string) $order_id], $order_insert["pay_price"]);
            $arr["pay_list"] = $jsApiParameters;
            $arr["order_id"] = $order_id;
        }
        return $this->success($arr);
    }
    public function getPayResellerData()
    {
        $config = getConfigSettingArr($this->_uniacid, ["reseller_threshold", "level_reseller_threshold"]);
        return $this->success($config);
    }
    public function resellerInfo()
    {
        $cap_dis[] = ["user_id", "=", $this->getUserId()];
        $cap_dis[] = ["status", "in", [1, 2, 3, 4]];
        $distribution_model = new \app\massage\model\DistributionList();
        $cap_info = $distribution_model->dataInfo($cap_dis);
        return $this->success($cap_info);
    }
    public function applyChannel()
    {
        $input = $this->_input;
        $distribution_model = new \app\massage\model\ChannelList();
        $dis[] = ["status", ">", -1];
        $dis[] = ["user_id", "=", $this->getUserId()];
        $find = $distribution_model->dataInfo($dis);
        if(!empty($find) && in_array($find["status"], [1, 2, 3])) {
            $this->errorMsg("你已经申请");
        }
        if(!empty($input["salesman_id"])) {
            $salesman_model = new \app\massage\model\Salesman();
            $salesman = $salesman_model->dataInfo(["id" => $input["salesman_id"], "status" => 2]);
            if(!empty($salesman)) {
                $salesman_id = $salesman["id"];
                $admin_id = $salesman["admin_id"];
                $inv_channel_balance = $salesman["inv_channel_balance"];
            }
        }
        if(!empty($input["admin_id"])) {
            $admin_model = new \app\massage\model\Admin();
            $admin = $admin_model->dataInfo(["id" => $input["admin_id"], "status" => 1]);
            if(!empty($admin)) {
                $admin_id = $input["admin_id"];
            }
        }
        $insert = ["uniacid" => $this->_uniacid, "user_id" => $this->getUserId(), "user_name" => $input["user_name"], "true_user_name" => !empty($input["true_user_name"]) ? $input["true_user_name"] : $input["user_name"], "mobile" => $input["mobile"], "cate_id" => $input["cate_id"], "text" => !empty($input["text"]) ? $input["text"] : "", "status" => 1, "salesman_id" => !empty($salesman_id) ? $salesman_id : 0, "admin_id" => !empty($admin_id) ? $admin_id : 0, "balance" => isset($inv_channel_balance) ? $inv_channel_balance : -1];
        \think\facade\Db::startTrans();
        if(!empty($find) && $find["status"] == 4) {
            $res = $distribution_model->dataUpdate(["id" => $find["id"]], $insert);
            $channel_id = $find["id"];
        } else {
            $res = $distribution_model->dataAdd($insert);
            $channel_id = $distribution_model->getLastInsID();
        }
        if(!empty($input["channel_qr_id"])) {
            $qr_model = new \app\massage\model\ChannelQr();
            $qr = $qr_model->dataInfo(["id" => $input["channel_qr_id"], "status" => 1]);
            if(!empty($qr)) {
                if(!empty($qr["channel_id"])) {
                    $channel = $distribution_model->where(["id" => $qr["channel_id"]])->where("status", ">", -1)->find();
                    if(!empty($channel)) {
                        $this->errorMsg("该码已经绑定渠道商");
                    }
                    $qr_model->dataUpdate(["id" => $input["channel_qr_id"]], ["channel_id" => 0]);
                }
                $update = ["salesman_id" => $qr["salesman_id"], "admin_id" => $qr["admin_id"], "sh_time" => time(), "status" => 2, "is_qr" => 1];
                $distribution_model->dataUpdate(["id" => $channel_id], $update);
                $res = $qr_model->dataUpdate(["id" => $qr["id"], "channel_id" => 0], ["channel_id" => $channel_id]);
                if($res == 0) {
                    \think\facade\Db::rollback();
                    $this->errorMsg("该码已经绑定渠道商");
                }
            } else {
                \think\facade\Db::rollback();
                $this->errorMsg("该码已经绑定渠道商");
            }
        }
        \think\facade\Db::commit();
        return $this->success($res);
    }
    public function channelInfo()
    {
        if(!$this->getUserId()) {
            return $this->success([]);
        }
        $cap_dis[] = ["user_id", "=", $this->getUserId()];
        $cap_dis[] = ["status", "in", [1, 2, 3, 4]];
        $distribution_model = new \app\massage\model\ChannelList();
        $cap_info = $distribution_model->where($cap_dis)->order("id desc")->find();
        return $this->success($cap_info);
    }
    public function channelCateSelect()
    {
        $cate_model = new \app\massage\model\ChannelCate();
        $dis = ["uniacid" => $this->_uniacid, "status" => 1];
        $data = $cate_model->where($dis)->select()->toArray();
        return $this->success($data);
    }
    public function sendShortMsg()
    {
        $input = $this->_input;
        $config = new \app\massage\model\ShortCodeConfig();
        $dis = ["uniacid" => $this->_uniacid, "phone" => $input["phone"]];
        $find = $this->model->dataInfo($dis);
        if(!empty($find)) {
        }
        $res = $config->sendSmsCode($input["phone"], $this->_uniacid);
        if(!empty($res["Message"]) && $res["Message"] == "OK") {
            return $this->success(1);
        }
        return $this->error($res["Message"]);
    }
    public function bindUserPhone()
    {
        $input = $this->_input;
        $dis = ["uniacid" => $this->_uniacid, "phone" => $input["phone"]];
        $find = $this->model->dataInfo($dis);
        $short_code = getCache($input["phone"], $this->_uniacid);
        if($input["short_code"] != $short_code) {
            return $this->error("验证码错误");
        }
        if(!empty($find)) {
            $this->model->dataUpdate($dis, ["phone" => ""]);
        }
        $res = $this->model->dataUpdate(["id" => $this->getUserId()], $dis);
        $user = $this->getUserInfo();
        $user["phone"] = $input["phone"];
        $key = "longbing_user_autograph_" . $user["id"];
        $key = md5($key);
        setCache($key, $user, 7200);
        return $this->success($res);
    }
    public function addFeedback()
    {
        $input = $this->request->only(["type_name", "order_code", "content", "images", "video_url"]);
        $rule = ["type_name" => "require", "content" => "require"];
        $validate = \think\facade\Validate::rule($rule);
        if(!$validate->check($input)) {
            return $this->error($validate->getError());
        }
        $input["coach_id"] = $this->getUserId();
        $input["uniacid"] = $this->_uniacid;
        if(!empty($input["images"])) {
            $input["images"] = json_encode($input["images"]);
        }
        $input["create_time"] = time();
        $cap_dis[] = ["user_id", "=", $this->getUserId()];
        $cap_dis[] = ["status", "in", [2]];
        $coach_model = new \app\massage\model\Coach();
        $coach_info = $coach_model->dataInfo($cap_dis);
        if(!empty($coach_info)) {
            $input["type"] = 2;
            $input["true_coach_id"] = $coach_info["id"];
        }
        $res = \app\massage\model\Feedback::insert($input);
        if($res) {
            return $this->success("");
        }
        return $this->error("提交失败");
    }
    public function listFeedback()
    {
        $input = $this->request->param();
        $limit = $this->request->param("limit", 10);
        $where = [];
        if(isset($input["status"]) && in_array($input["status"], [1, 2])) {
            $where[] = ["a.status", "=", $input["status"]];
        }
        $where[] = ["a.coach_id", "=", $this->getUserId()];
        $where[] = ["a.uniacid", "=", $this->_uniacid];
        $data = \app\massage\model\Feedback::getList($where, $limit);
        $data["wait"] = \app\massage\model\Feedback::where(["coach_id" => $this->getUserId(), "uniacid" => $this->_uniacid, "status" => 1])->count();
        return $this->success($data);
    }
    public function feedbackInfo()
    {
        $id = $this->request->param("id");
        if(empty($id)) {
            return $this->error("参数错误");
        }
        $data = \app\massage\model\Feedback::getInfo(["a.id" => $id]);
        return $this->success($data);
    }
    public function delUserInfo()
    {
        if(!$this->getUserId()) {
            $this->errorMsg("请先登录");
        }
        $order_model = new \app\massage\model\Order();
        $order = $order_model->where(["user_id" => $this->getUserId()])->where("pay_type", "not in", [-1, 7])->find();
        if(!empty($order)) {
            $this->errorMsg("你还有订单未完成");
        }
        $cap_dis[] = ["user_id", "=", $this->getUserId()];
        $cap_dis[] = ["status", ">", -1];
        $cap_info = $this->coach_model->dataInfo($cap_dis);
        if(!empty($cap_info)) {
            $this->errorMsg("技师不能注销");
        }
        $channel_model = new \app\massage\model\ChannelList();
        $channel = $channel_model->dataInfo($cap_dis);
        if(!empty($channel)) {
            $this->errorMsg("渠道商不能注销");
        }
        $salesman_model = new \app\massage\model\Salesman();
        $salesman = $salesman_model->dataInfo($cap_dis);
        if(!empty($salesman)) {
            $this->errorMsg("业务员不能注销");
        }
        $admin_model = new \app\massage\model\Admin();
        $admin = $admin_model->dataInfo($cap_dis);
        if(!empty($admin)) {
            $this->errorMsg("代理商不能注销");
        }
        $reseller_model = new \app\massage\model\DistributionList();
        $reseller = $reseller_model->dataInfo($cap_dis);
        if(!empty($reseller)) {
            $this->errorMsg("分销员不能注销");
        }
        \think\facade\Db::startTrans();
        $user_info = $this->model->dataInfo(["id" => $this->_user["id"]]);
        $open_id = $this->getUserInfo()["openid"] . time();
        $res = $this->model->dataUpdate(["id" => $this->getUserId()], ["status" => -1, "openid" => $open_id]);
        if($res == 0) {
            \think\facade\Db::rollback();
            $this->errorMsg("注销失败");
        }
        $dis = ["openid" => $user_info["web_openid"]];
        if(!empty($user_info["unionid"])) {
            $dis["unionid"] = $user_info["unionid"];
        }
        $bind_model = new \app\massage\model\QrBind();
        $bind_model->whereOr($dis)->delete();
        $where = ["open_id" => $user_info["web_openid"]];
        if(!empty($user_info["unionid"])) {
            $where["unionid"] = $user_info["unionid"];
        }
        $channel_model = new \app\massage\model\UserChannel();
        $channel_model->whereOr($where)->delete();
        if(!empty($user_info["web_openid"])) {
            $scan_model = new \app\massage\model\ChannelScanQr();
            $scan_model->where(["open_id" => $user_info["web_openid"], "user_id" => 0])->update(["user_id" => $user_info["id"]]);
        }
        \think\facade\Db::commit();
        setCache($this->autograph, "", 0);
        return $this->success(true);
    }
    public function shieldCoachAdd()
    {
        $input = $this->_input;
        $dis = ["coach_id" => $input["coach_id"], "user_id" => $this->_user_id, "type" => $input["type"], "uniacid" => $this->_uniacid];
        $shield_model = new \app\massage\model\ShieldList();
        $find = $shield_model->dataInfo($dis);
        if(empty($find)) {
            $shield_model->dataAdd($dis);
        }
        return $this->success(true);
    }
    public function shieldCoachDel()
    {
        $input = $this->_input;
        $dis = ["id" => $input["id"]];
        $shield_model = new \app\massage\model\ShieldList();
        $res = $shield_model->where($dis)->delete();
        return $this->success($res);
    }
    public function shieldCoachList()
    {
        $input = $this->_param;
        $dis = ["a.user_id" => $this->_user_id, "a.type" => $input["type"]];
        $shield_model = new \app\massage\model\ShieldList();
        $res = $shield_model->dataList($dis);
        return $this->success($res);
    }
    public function bindAlipayNumber()
    {
        $input = $this->_input;
        $dis = ["id" => $this->_user_id];
        $res = $this->model->dataUpdate($dis, ["alipay_number" => $input["alipay_number"], "alipay_name" => $input["alipay_name"]]);
        return $this->success($res);
    }
    public function getVirtualPhone()
    {
        $input = $this->_param;
        $phone = !empty($input["phone"]) ? $input["phone"] : 0;
        $order_model = new \app\massage\model\Order();
        $order = $order_model->dataInfo(["id" => $input["order_id"]]);
        if(in_array($order["pay_type"], [-1, 7])) {
            return $this->error("接单已结束");
        }
        $called = new \app\virtual\model\Config();
        $res = $called->getVirtual($order, 2, $phone);
        return $this->success($res);
    }
    public function getStoreSelect()
    {
        $input = $this->_param;
        $store_model = new \app\massage\model\StoreList();
        $dis = ["uniacid" => $this->_uniacid, "status" => 1];
        if(!empty($input["admin_id"])) {
            $dis["admin_id"] = $input["admin_id"];
        }
        $data = $store_model->where($dis)->field("id,title")->select()->toArray();
        return $this->success($data);
    }
    public function applySalesman()
    {
        $input = $this->_input;
        $salesman_model = new \app\massage\model\Salesman();
        $dis[] = ["user_id", "=", $this->getUserId()];
        $dis[] = ["status", ">", -1];
        $info = $salesman_model->dataInfo($dis);
        if(!empty($info) && in_array($info["status"], [1, 2, 3])) {
            $this->errorMsg("你已经申请过分销员了");
        }
        $status = 1;
        $sh_time = 0;
        $insert = ["uniacid" => $this->_uniacid, "user_id" => $this->getUserId(), "admin_id" => $input["admin_id"], "phone" => $input["phone"], "user_name" => $input["user_name"], "true_user_name" => !empty($input["true_user_name"]) ? $input["true_user_name"] : $input["user_name"], "status" => $status, "sh_time" => $sh_time];
        $res = $salesman_model->dataAdd($insert);
        return $this->success($res);
    }
    public function salesmanInfo()
    {
        $input = $this->_param;
        $salesman_model = new \app\massage\model\Salesman();
        $admin_model = new \app\massage\model\Admin();
        $cap_dis[] = ["user_id", "=", $this->getUserId()];
        $cap_dis[] = ["status", "in", [1, 2, 3, 4]];
        $data = $salesman_model->where($cap_dis)->order("id desc")->find();
        if(!empty($data)) {
            $data = $data->toArray();
            $data["admin_name"] = $admin_model->where(["id" => $data["admin_id"], "status" => 1])->value("agent_name");
        }
        return $this->success($data);
    }
    public function agentApply()
    {
        $input = $this->_input;
        $agent_model = new \app\massage\model\AgentApply();
        $input["uniacid"] = $this->_uniacid;
        $input["user_id"] = $this->_user["id"];
        if(!empty($input["short_code"])) {
            $short_code = getCache($input["phone"], $this->_uniacid);
            if($input["short_code"] != $short_code) {
                return $this->error("验证码错误");
            }
            unset($input["short_code"]);
            setCache($input["phone"], "", 99, $this->_uniacid);
        }
        $res = $agent_model->dataAdd($input);
        return $this->success($res);
    }
    public function bindChannel()
    {
        $input = $this->_input;
        $channel_model = new \app\massage\model\UserChannel();
        $user_channel_over_time = getConfigSetting($this->_uniacid, "user_channel_over_time");
        $auth = \longbingcore\permissions\AdminMenu::getAuthList((int) $this->_uniacid, ["channelforeverbind"]);
        $channel_bind_type = getConfigSetting((int) $this->_uniacid, "channel_bind_type");
        $channel_bind_forever = $auth["channelforeverbind"];
        $dis = ["user_id" => $this->_user["id"], "open_id" => $this->_user["openid"]];
        if($channel_bind_type == 1) {
            if($channel_bind_forever) {
                $find = $channel_model->whereOr($dis)->find();
                $time = strtotime(date("Y-m-d", time()));
                if($this->_user["create_time"] < $time) {
                    $find = 1;
                }
            } else {
                $res = $channel_model->whereOr($dis)->delete();
            }
        } elseif($channel_bind_forever) {
            $find = $channel_model->whereOr($dis)->find();
        } else {
            $find = $channel_model->where("over_time", ">", time())->where(function ($query) use ($dis) {
                $query->whereOr($dis);
            })->find();
            if(empty($find)) {
                $channel_model->whereOr($dis)->delete();
            }
        }
        if(!empty($input["channel_id"])) {
            $channels_model = new \app\massage\model\ChannelList();
            $channel = $channels_model->where(["id" => $input["channel_id"]])->where("status", "in", [2, 3])->find();
            if(!empty($channel["time_type"]) && $channel["time_type"] == 1) {
                $user_channel_over_time = $channel["time"];
            }
        }
        if(empty($find)) {
            $insert = ["uniacid" => $this->_uniacid, "user_id" => $this->_user["id"], "channel_id" => $input["channel_id"], "over_time" => time() + $user_channel_over_time * 3600];
            $res = $channel_model->dataAdd($insert);
            return $this->success($res);
        }
        return $this->success(true);
    }
    public function getWecomStaff()
    {
        $config = longbingGetAppConfig($this->_uniacid, true);
        if(!empty($config["wecom_staff"])) {
            $wecom_staff = unserialize($config["wecom_staff"]);
            if(!empty($wecom_staff)) {
                $key = array_rand($wecom_staff);
                $data = $wecom_staff[$key];
                return $this->success($data);
            }
        }
        return $this->success("");
    }
    public function addScanRecord()
    {
        $input = $this->_input;
        $scan_model = new \app\massage\model\ChannelScanQr();
        $insert = ["uniacid" => $this->_uniacid, "type" => $input["type"], "qr_id" => $input["qr_id"], "is_qr" => isset($input["is_qr"]) ? $input["is_qr"] : 1, "admin_id" => !empty($input["admin_id"]) ? $input["admin_id"] : 0];
        if($this->getUserInfo()) {
            $insert["user_id"] = $this->_user["id"];
            $insert["open_id"] = $this->_user["openid"];
        }
        $scan_model->dataAdd($insert);
        $id = $scan_model->getLastInsID();
        $id = $this->getUserInfo() ? 0 : $id;
        return $this->success($id);
    }
    public function updateScanRecord()
    {
        $input = $this->_input;
        $scan_model = new \app\massage\model\ChannelScanQr();
        $is_register = !empty($input["is_register"]) ? $input["is_register"] : 0;
        $insert["user_id"] = $this->_user["id"];
        $insert["open_id"] = $this->_user["openid"];
        $insert["is_new"] = $is_register;
        $res = $scan_model->dataUpdate(["id" => $input["id"]], $insert);
        if($is_register == 1) {
            $qr_find = $scan_model->dataInfo(["id" => $input["id"]]);
            if(!empty($qr_find)) {
                $update = ["source_type" => $qr_find["type"], "is_qr" => $qr_find["is_qr"], "admin_id" => !empty($qr_find["admin_id"]) ? $qr_find["admin_id"] : $scan_model->getQrAdminId($qr_find)];
                $this->model->dataUpdate(["id" => $this->_user["id"]], $update);
            }
        }
        return $this->success($res);
    }
    public function applyBroker()
    {
        $input = $this->_input;
        $broker_model = new \app\coachbroker\model\CoachBroker();
        $dis[] = ["user_id", "=", $this->getUserId()];
        $dis[] = ["status", ">", -1];
        $info = $broker_model->dataInfo($dis);
        if(!empty($info) && in_array($info["status"], [1, 2, 3])) {
            $this->errorMsg("你已经申请过经纪人了");
        }
        $insert = ["uniacid" => $this->_uniacid, "user_id" => $this->getUserId(), "user_name" => $input["user_name"], "true_user_name" => !empty($input["true_user_name"]) ? $input["true_user_name"] : $input["user_name"], "mobile" => $input["mobile"], "text" => $input["text"]];
        if(!empty($info) && $info["status"] == 4) {
            $insert["status"] = 1;
            $res = $broker_model->dataUpdate(["id" => $info["id"]], $insert);
        } else {
            $res = $broker_model->dataAdd($insert);
        }
        return $this->success($res);
    }
    public function brokerInfo()
    {
        $input = $this->_param;
        $dis[] = ["user_id", "=", $this->getUserId()];
        $dis[] = ["status", "in", [1, 2, 3, 4]];
        $broker_model = new \app\coachbroker\model\CoachBroker();
        $info = $broker_model->dataInfo($dis);
        if(!empty($info)) {
            $user_model = new \app\massage\model\User();
            $info["nickName"] = $user_model->where(["id" => $info["user_id"]])->value("nickName");
        }
        return $this->success($info);
    }
    public function getAddressByIp()
    {
        return $this->success(getAddressByIp($this->_uniacid));
    }
    public function getAccountCoach()
    {
        $coach_model = new \app\massage\model\Coach();
        $dis[] = ["status", "in", [1, 2, 3]];
        $dis[] = ["user_id", "=", $this->_user["id"]];
        $info = $coach_model->where($dis)->field("id,coach_name,work_img")->find();
        if(!empty($info)) {
            $account_model = new \app\massage\model\CoachAccount();
            $info["account_info"] = $account_model->dataInfo(["coach_id" => $info["id"], "status" => 1]);
        }
        return $this->success($info);
    }
    public function coachTypeSelect()
    {
        $input = $this->_param;
        $type_model = new \app\massage\model\CoachType();
        $dis[] = ["uniacid", "=", $this->_uniacid];
        $dis[] = ["status", "=", 1];
        $data = $type_model->where($dis)->order("top desc,id desc")->select()->toArray();
        return $this->success($data);
    }
    public function adminList()
    {
        $admin_model = new \app\massage\model\Admin();
        $dis = ["status" => 1, "is_admin" => 0, "sub_agent_auth" => 1];
        $input = $this->_param;
        $where = [];
        if(!empty($input["nickName"])) {
            $where[] = ["agent_name", "like", "%" . $input["nickName"] . "%"];
        }
        $data = $admin_model->where($dis)->where($where)->field("agent_name,id,city_type,user_id")->paginate(10)->toArray();
        $user_model = new \app\massage\model\User();
        if(!empty($data["data"])) {
            foreach ($data["data"] as &$v) {
                $v["avatarUrl"] = $user_model->where(["id" => $v["user_id"]])->value("avatarUrl");
            }
        }
        return $this->success($data);
    }
    public function adminAuth()
    {
        $admin_model = new \app\massage\model\Admin();
        $admin_where = ["user_id" => $this->_user["id"], "status" => 1];
        $admin_user = $admin_model->dataInfo($admin_where);
        $data["is_admin"] = !empty($admin_user) ? 1 : 0;
        $data["phone"] = $this->model->where(["id" => $this->_user["id"]])->value("phone");
        $input = $this->_param;
        if(!empty($input["admin_id"])) {
            $data["top_name"] = $admin_model->where(["id" => $input["admin_id"], "status" => 1])->value("agent_name");
        }
        return $this->success($data);
    }
    public function industryTypeSelect()
    {
        $model = new \app\industrytype\model\Type();
        $model->initData($this->_uniacid);
        $input = $this->_param;
        $dis[] = ["uniacid", "=", $this->_uniacid];
        if(isset($input["status"])) {
            $dis[] = ["status", "=", $input["status"]];
        } else {
            $dis[] = ["status", "=", 1];
        }
        if(!empty($input["title"])) {
            $dis[] = ["title", "like", "%" . $input["title"] . "%"];
        }
        if($this->is_app == 0 && getConfigSetting($this->_uniacid, "shield_massage") == 1) {
            $dis[] = ["type", "<>", 1];
        }
        $data = $model->where($dis)->order("top desc,id desc")->select()->toArray();
        return $this->success($data);
    }
    public function stationIconSelect()
    {
        $input = $this->_param;
        $model = new \app\massage\model\StationIcon();
        $dis[] = ["uniacid", "=", $this->_uniacid];
        $dis[] = ["status", "=", 1];
        if(!empty($input["industry_type"])) {
            $dis[] = ["industry_type", "=", $input["industry_type"]];
        }
        $data = $model->where($dis)->order("id desc")->select()->toArray();
        return $this->success($data);
    }
    public function coachIconSelect()
    {
        $input = $this->_param;
        $model = new \app\massage\model\IconCoach();
        $dis[] = ["uniacid", "=", $this->_uniacid];
        $dis[] = ["status", "=", 1];
        $data = $model->where($dis)->order("id desc")->select()->toArray();
        return $this->success($data);
    }
}

?>